package herguan.expertadvise.web.struts.action;
import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;

import herguan.expertadvise.dao.SpringHibernateDAO;
import herguan.expertadvise.dao.hibernate.Skill;
import herguan.expertadvise.services.ExpertAdviseServiceFinder;
import herguan.expertadvise.vo.UsersListVO;
import herguan.expertadvise.web.struts.form.DisplaySkillForm;
import herguan.expertadvise.web.struts.form.UserLoginForm;

public class DisplaySkillAction extends Action 
{
	String strProviderid = "1";
	 public ActionForward execute(
			    ActionMapping mapping,
			    ActionForm form,
			    HttpServletRequest request,
			    HttpServletResponse response) throws Exception{
				  
		       
	       
		        //Retrieve the DAO Reference
		  	  SpringHibernateDAO springHibernateDAO = (SpringHibernateDAO) ExpertAdviseServiceFinder.getContext(request)
		  		.getBean("SpringHibernateDao");
		  	  
		  	
//		  	Create object of ActionMesssages
		        ActionMessages errors = new ActionMessages();
		  	  
		        DisplaySkillForm objForm = (DisplaySkillForm) form;
		        ArrayList<Skill> objSkill = null;
		        objSkill = springHibernateDAO.loadSkills();
		        
		        String[] values =new String[objSkill.size()];
		        for(int i=0;i< objSkill.size();i++)
		        {
					values[i] = (String)objSkill.get(i).getSkillname();
					
		           
		        }
				objForm.setSkill(objSkill);
				objForm.setItem(values);
				
		        int count=0;
		        values=objForm.getItem();
		        if(values!=null)
		        {
		        for(int i=0;i< values.length;i++)
		        {
		                count++;
		           
		        }
		        }
		        objForm.setNumber(count);

		        
		        request.setAttribute("DisplaySkillForm", form);
		        request.setAttribute("collection", values);
		        //Save the htmlString in the session for later...
		        HttpSession session = request.getSession();
		        session.setAttribute("skills", values);
		        if (objForm.getAction().equals("add")) 
		        { 	
		        	
			        
			             return mapping.findForward("success");
		        }
		        
		        if (objForm.getAction().equals("select")) 
		        { 	
		       
				/*for(int i=0;i< objSkill.size();i++)
		        {
					values[i] = (String)objSkill.get(i).getSkillname();
					
		           
		        }
				objForm.setSkill(objSkill);*/
				
				String select[] = request.getParameterValues("optionskill"); 
				ArrayList<String> arrSelectedSkills = new ArrayList<String>();
				
				if (select != null && select.length != 0) {
				System.out.println("You have selected: ");
				for (int i = 0; i < select.length; i++) {
			    System.out.println(select[i]); 
			    	arrSelectedSkills.add(select[i]);
				}
				}
				
				ArrayList<UsersListVO> objUserList = null;
				objUserList = (ArrayList<UsersListVO>) springHibernateDAO.getUsersList(arrSelectedSkills);
				 
				 String[] arrskilleduser =new String[objUserList.size()];
				 for(int i=0;i< objUserList.size();i++)
			        {
					 arrskilleduser[i] = (String)objUserList.get(i).getLoginid();
						
			           
			        }
				session.setAttribute("skilleduser", objUserList);
				 
				/*int count=0;
		        values=objForm.getItem();
		        if(values!=null)
		        {
		        for(int i=0;i< values.length;i++)
		        {
		                count++;
		           
		        }
		        }
		        objForm.setNumber(count);
		        
		        request.setAttribute("DisplaySkillForm", form);
		        request.setAttribute("collection", values);
		        //Save the htmlString in the session for later...
		        HttpSession session = request.getSession();
		        session.setAttribute("skills", values);
		        
		        objForm.setItem(values);*/
		             return mapping.findForward("displayuser");
		        }
		        if (objForm.getAction().equals("checkoutuser")) 
		        { 	 
		        	strProviderid= request.getParameterValues("radiouser")[0];
		        	request.setAttribute("DisplaySkillForm", form);
		        	return mapping.findForward("checkoutuser");
		        	
		        }
		        if (objForm.getAction().equals("proceedquestion")) 
		        	
		        { 	
		        
			           String struserid=session.getAttribute("ID").toString();
		        	int seekerid = Integer.parseInt(struserid);
		              int providerid=Integer.parseInt(strProviderid);
		              String question= objForm.getQuestion();
		        	springHibernateDAO.addquestion(seekerid,providerid,question);
		        	request.setAttribute("DisplaySkillForm", form);
		        	return mapping.findForward("proceedquestion");
		        	
		        	
		        }
		        
		        
		        
		        return mapping.findForward("success");
	       }
}
